'use strict';

angular.module('app')
	.controller('inAndOutManagerController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster',
	                                function($scope, $rootScope, $state, $timeout, toaster) {
		$scope.title = '开门记录';
        $scope.param = { };
        $scope.loading = false;
		$scope.projectId = localStorage.getItem('projectId');//项目类型标识
		$scope.search = function () {
			//时间判断
			if($("#startTime").val() && $("#endTime").val()){
				if(new Date($("#startTime").val()) > new Date($("#endTime").val())){
					toaster.clear('*');
		         	toaster.pop('error', '', '开始时间不能大于结束时间');
		         	return;
				}
			}
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
		  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
		  	}else{//点击翻页
		  	   $scope.pageFlag = 2;//将开关值设置为2
		  	}
		  	
			if($("#startTime").val()){
				$scope.param.startTime = $("#startTime").val();
			}else{
				$scope.param.startTime = '';
			}
			if($("#endTime").val()){
				$scope.param.endTime = $("#endTime").val();
			}else{
				$scope.param.endTime = '';
			}
			$.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : 'dac/authm/cardAuth/inAndOutlist',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
					toaster.clear('*');
					toaster.pop('error','',$scope.msg)
				}
				$scope.$apply();
			});
	        $scope.startTime = false;
        	$scope.endTime = false;
		}
		//从本地获取localstorage数据，对权限按钮做隐藏
		var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
		personAuth = personAuth.split(',');
		for(var i = 0;i<personAuth.length;i++){
			if(personAuth[i]=='main.authm.cardAuth.inAndOut.read'){
				$scope.read = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.inAndOut.add'){
				$scope.add = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.inAndOut.update'){
				$scope.update = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.inAndOut.delete'){
				$scope.dele = true;
			}
		}
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.disableItem = function(id, enable) {
			
		}
			
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
        
        //时间选项
		$scope.timeOptions = {
			startingDay: 1,
			class: 'datepicker'
		};
		//日期控件
		$scope.openDatepicker = function($event, i) {
			$event.preventDefault();
			$event.stopPropagation();
			switch(i){
				case 1:
				$scope.startTime = true;
				break;
				case 2:
				$scope.endTime = true;
				break;
				default:
				return;
			}
		}
} ]);